package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.PropResponse;
import com.baihan.angus.app.game.common.util.ImageLoader;

public class PropDetailDialog extends Dialog {

    private Callback mCallback;
    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private TextView tvTransfer, tvUse;
    private ImageView ivClose, ivProp;

    private PropResponse.DataX.Data data;

    public PropDetailDialog(@NonNull Context context, Callback mCallback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = mCallback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
//        mLayoutParams.height = AppUtil.getScreenHeight(context);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.CENTER);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_prop_detail, (ViewGroup) null);
        setContentView(dialogView);
        tvTransfer = dialogView.findViewById(R.id.tvTransfer);
        tvUse = dialogView.findViewById(R.id.tvUse);
        ivProp = dialogView.findViewById(R.id.ivProp);
        ivClose = dialogView.findViewById(R.id.ivClose);

        tvTransfer.setOnClickListener(view -> {
            if (null != mCallback) {
                mCallback.onTransfer(data.getScript_relevant_id());
            }
        });

        tvUse.setOnClickListener(view -> {
            if (null != mCallback) {
                mCallback.onUse(data.getRoom_prop_id(), data.getUse_type());
            }
        });

        ivClose.setOnClickListener(v -> {
            cancel();
        });
    }

    public void initPropData(PropResponse.DataX.Data propData) {
        data = propData;
        ImageLoader.loadImage(mContext, ivProp, propData.getUrl().get(0).getUrl(), R.mipmap.ic_test_bg, 0);

        if (propData.getIs_use() == 0) {//0-未使用 1-已使用
            tvTransfer.setVisibility(View.VISIBLE);
            tvUse.setVisibility(View.VISIBLE);
        } else if (propData.getIs_use() == 1) {
            tvTransfer.setVisibility(View.GONE);
            tvUse.setVisibility(View.GONE);
        }
    }

    public interface Callback {
        void onTransfer(String propId);

        void onUse(String propId, int useType);

    }
}
